package com.harmontronics.hanma.paas.paas.domain.hmureport.service;

import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import com.harmontronics.hanma.paas.paas.domain.hmureport.entity.HmUreportFile;
import com.harmontronics.hanma.paas.paas.utils.RequestUtils;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyfileReportProvider implements ReportProvider {

    private static final String NAME = "postgres-provider";

    private String prefix="postgres:";
    private String fileStoreDir;
    private boolean disabled;

    @Autowired
    private HmUreportFileService hmUreportFileService;

    @Override
    public InputStream loadReport(String file) {

        HmUreportFile hmUreportFile = hmUreportFileService.queryUreportFileEntityByName(getCorrectName(file));
        byte[] content = hmUreportFile.getFileContent();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
        return inputStream;
    }

    @Override
    public void deleteReport(String file) {
        hmUreportFileService.deleteReportFileByName(getCorrectName(file));
    }

    @Override
    public List<ReportFile> getReportFiles() {
        List<HmUreportFile> list = hmUreportFileService.getList();
        List<ReportFile> reportFileList = new ArrayList<>();
        for(HmUreportFile ureportFile : list){
            Date updateDate = Date.from(ureportFile.getModifyDate().atZone(ZoneId.systemDefault()).toInstant());
            reportFileList.add(new ReportFile(ureportFile.getFileName(),updateDate));
        }

        return reportFileList;
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public void saveReport(String file,String content) {
        String fileName = getCorrectName(file);
        HmUreportFile hmUreportFile = hmUreportFileService.queryUreportFileEntityByName(fileName);
        if(hmUreportFile.getId() == null){
            hmUreportFile = new HmUreportFile();
            hmUreportFile.setFileName(fileName);
            hmUreportFile.setFileContent(content.getBytes());
            hmUreportFile.setCreateDate(LocalDateTime.now());
            hmUreportFile.setModifyDate(LocalDateTime.now());
            hmUreportFileService.saveForReport(hmUreportFile);
        }else{
            hmUreportFile.setFileContent(content.getBytes());
            hmUreportFile.setModifyDate(LocalDateTime.now());
            hmUreportFileService.updateForReport(hmUreportFile);
        }

    }

    @Override
    public boolean disabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public void setFileStoreDir(String fileStoreDir) {
        this.fileStoreDir = fileStoreDir;
    }


    @Override
    public String getPrefix() {
        return prefix;
    }

    /**
     * 获取没有前缀的文件名
     * @param name
     * @return
     */
    private String getCorrectName(String name){
        if(name.startsWith(prefix)){
            name = name.substring(prefix.length(), name.length());
        }
        return name;
    }

}
